package com.linkage.educloud.ah.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.view.BasicActivity;
import com.linkage.educloud.ah.view.fragment.AppFragment;
import com.linkage.educloud.ah.view.fragment.ContactFragment;
import com.linkage.educloud.ah.view.fragment.HomeFragment;
import com.linkage.educloud.ah.view.fragment.MineFragment;
import com.linkage.educloud.ah.view.fragment.MessageFragment;
import com.zzhou.common.library.util.ToastShow;

import butterknife.BindView;


public class MainActivity extends BasicActivity implements BottomNavigationBar.OnTabSelectedListener {


    @BindView(R.id.fragment_content)
    FrameLayout fragmentContent;
    @BindView(R.id.activity_main_bottombar)
    BottomNavigationBar navigationBar;

    private FragmentManager mFragmentManager;
    private Fragment[] mFragments = new Fragment[5];
    private int curIndex;

    public static void launch(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragmentManager = getSupportFragmentManager();
        initFragment();
        initBottomBar();
    }

    private void initFragment() {
        mFragments[0] = HomeFragment.newInstance();
        mFragments[1] = MessageFragment.newInstance();
        mFragments[2] = ContactFragment.newInstance();
        mFragments[3] = AppFragment.newInstance();
        mFragments[4] = MineFragment.newInstance();
        setDefaultFragment(curIndex);
    }

    private void initBottomBar() {
        navigationBar.clearAll();
        navigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        navigationBar.setBarBackgroundColor(R.color.white_f0);
        navigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        navigationBar.setActiveColor(R.color.dark_green).setInActiveColor(R.color.tab_color_false);

        navigationBar
                .addItem(new BottomNavigationItem(R.drawable.tab_home_icon, R.string.tab_home_txt))
                .addItem(new BottomNavigationItem(R.drawable.tab_message_icon, R.string.tab_message_txt))
                .addItem(new BottomNavigationItem(R.drawable.tab_contact_icon, R.string.tab_contact_txt))
                .addItem(new BottomNavigationItem(R.drawable.tab_learn_icon, R.string.tab_app_txt_teacher))
                .addItem(new BottomNavigationItem(R.drawable.tab_mine_icon, R.string.tab_mine_txt)).initialise();
        navigationBar.setTabSelectedListener(this);
    }

    private void setDefaultFragment(int position) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.fragment_content, mFragments[position]);
        transaction.commit();
    }

    @Override
    public void onTabSelected(int position) {
        showHideFragment(curIndex = position);
    }

    @Override
    public void onTabUnselected(int position) {
        if (mFragments != null) {
            if (position < mFragments.length) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Fragment fragment = mFragments[position];
                // 隐藏当前的fragment
                ft.hide(fragment);
                ft.commitAllowingStateLoss();
            }
        }
    }

    @Override
    public void onTabReselected(int position) {

    }

    private void showHideFragment(int position) {
        try {
            if (mFragments != null) {
                if (position < mFragments.length) {
                    FragmentTransaction ft = mFragmentManager.beginTransaction();
                    //当前的fragment
                    Fragment from = mFragmentManager.findFragmentById(R.id.fragment_content);
                    //点击即将跳转的fragment
                    Fragment fragment = mFragments[position];
                    if (fragment.isAdded()) {
                        // 隐藏当前的fragment，显示下一个
                        ft.hide(from).show(fragment);
                    } else {
                        ft.hide(from).add(R.id.fragment_content, fragment);
                    }
                    ft.commitAllowingStateLoss();
                }
            }
        } catch (Exception e) {
        }
    }

    //直通车点击事件
    public void zhitongche(View view) {
        ToastShow.show(this, "直通车界面待添加");
    }
}
